package com.ruoyi.credential.controller;

import java.util.List;
import javax.servlet.http.HttpServletResponse;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.credential.domain.KwUserCredential;
import com.ruoyi.credential.service.IKwUserCredentialService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 用户凭证信息Controller
 * 
 * @author gonianer
 * @date 2023-11-15
 */
@RestController
@RequestMapping("/credential/credential")
public class KwUserCredentialController extends BaseController
{
    @Autowired
    private IKwUserCredentialService kwUserCredentialService;

    /**
     * 查询用户凭证信息列表
     */
    @PreAuthorize("@ss.hasPermi('credential:credential:list')")
    @GetMapping("/list")
    public TableDataInfo list(KwUserCredential kwUserCredential)
    {
        startPage();
        List<KwUserCredential> list = kwUserCredentialService.selectKwUserCredentialList(kwUserCredential);
        return getDataTable(list);
    }

    /**
     * 导出用户凭证信息列表
     */
    @PreAuthorize("@ss.hasPermi('credential:credential:export')")
    @Log(title = "用户凭证信息", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, KwUserCredential kwUserCredential)
    {
        List<KwUserCredential> list = kwUserCredentialService.selectKwUserCredentialList(kwUserCredential);
        ExcelUtil<KwUserCredential> util = new ExcelUtil<KwUserCredential>(KwUserCredential.class);
        util.exportExcel(response, list, "用户凭证信息数据");
    }

    /**
     * 获取用户凭证信息详细信息
     */
    @PreAuthorize("@ss.hasPermi('credential:credential:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(kwUserCredentialService.selectKwUserCredentialById(id));
    }

    /**
     * 新增用户凭证信息
     */
    @PreAuthorize("@ss.hasPermi('credential:credential:add')")
    @Log(title = "用户凭证信息", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody KwUserCredential kwUserCredential)
    {
        kwUserCredential.setUserId(getUserId());
        int rows = kwUserCredentialService.insertKwUserCredential(kwUserCredential);
        if(rows > 0) {
            return success(kwUserCredential.getId());
        }
        return toAjax(rows);
    }

    /**
     * 修改用户凭证信息
     */
    @PreAuthorize("@ss.hasPermi('credential:credential:edit')")
    @Log(title = "用户凭证信息", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody KwUserCredential kwUserCredential)
    {
        return toAjax(kwUserCredentialService.updateKwUserCredential(kwUserCredential));
    }

    /**
     * 删除用户凭证信息
     */
    @PreAuthorize("@ss.hasPermi('credential:credential:remove')")
    @Log(title = "用户凭证信息", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(kwUserCredentialService.deleteKwUserCredentialByIds(ids));
    }
}
